home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS04.ADF / C / setparallel.c < prev    next >
C/C++ Source or Header  |  1985-12-04  |  6KB  |  203 lines

  1. /* SetParallel allows the CLI user to dynamically change any particular
  2.  * parallel port parameter.
  3.  *
  4.  * July 6, 1985  Version 1.0  Keith Stobie  Initial version (serial)
  5.  * Aug 16, 1985  Version 1.1  Tom Pohorsky  cloned for parallel
  6.  *
  7.  */
  8. char *prog_name;            /* Name of program being run */
  9. char *prog_version = "1.0"; /* Version of the program */
  10.  
  11. #include        <exec/types.h>
  12. #include        <exec/nodes.h>
  13. #include        <exec/lists.h>
  14. #include        <exec/memory.h>
  15. #include        <exec/ports.h>
  16. #include        <exec/libraries.h>
  17. #include        <exec/devices.h>
  18. #include        <exec/tasks.h>
  19. #include        <exec/io.h>
  20. #include        <devices/parallel.h>
  21.  
  22.  
  23.  
  24. extern struct MsgPort *CreatePort();
  25.  
  26. set_parallel( index, value )
  27.    int   index;         /* which parameter to set */
  28.    ULONG value;      /* The new value for paramter */
  29. {
  30.     struct IOExtPar IORpar;      /* Parallel port IO request block */
  31.     int     error = 0;
  32.     int     return_code = 0;
  33.     
  34.     IORpar.io_ParFlags |= PARF_SHARED;
  35.     if ((error = OpenDevice (PARALLELNAME, 0, &IORpar, 0)) != 0) {
  36.         printf( "Unable to open Parallel Device, error=%ld\n", error );
  37.         return 20;
  38.     }
  39.  
  40.     IORpar.IOPar.io_Flags   = 0;
  41.     IORpar.IOPar.io_Command = PDCMD_QUERY;
  42.     if ((error = DoIO( &IORpar )) != 0) {
  43.         printf( "Parallel Device query error=%ld\n", error );
  44.         return 20;
  45.     }
  46.  
  47.     /* SET UP the read message port in the I/O request */
  48.     if ((IORpar.IOPar.io_Message.mn_ReplyPort = CreatePort( "SetParallel", 0 ))
  49.          == NULL) {
  50.         printf( "Unable to create port for IO message\n" );
  51.         CloseDevice( &IORpar );
  52.         return 20;
  53.       }
  54.  
  55.     switch( index ) {
  56.       case 0:    print_request( &IORpar );         break;
  57.       case 1:    IORpar.io_ParFlags      = value;  break;
  58.       case 2:    IORpar.io_PTermArray.PTermArray0 = value;
  59.                  /* ARRAY CAN BE SET ONLY IF EOFMODE */
  60.                  /* SO SET MODE (x02) FROM CASE 1 */
  61.                  /* fill out array with lowest valid value */
  62.                  value = value & 0xFF;
  63.                  IORpar.io_PTermArray.PTermArray1 
  64.                      = value<<24 & value<<16 & value<<8 & value;
  65.                  break;
  66.  
  67.       default:   printf( "Index value %ld is not in range 0-2!\n", index );
  68.                  IORpar.IOPar.io_Error = 0;
  69.                  return_code = 10;   
  70.                  goto cleanup;
  71.     } /* switch */
  72.  
  73.     IORpar.IOPar.io_Flags   = 0;
  74.     IORpar.IOPar.io_Command = PDCMD_SETPARAMS;
  75.     error = DoIO( &IORpar );
  76. cleanup:
  77.     DeletePort( IORpar.IOPar.io_Message.mn_ReplyPort );
  78.     CloseDevice( &IORpar );
  79.     if (error) {
  80.        printf( "Error %ld doing IO to set params!\n", error );
  81.        return 10;
  82.     }
  83.       
  84.     if (IORpar.IOPar.io_Error) {
  85.        printf( "Error %ld from parallel device doing set params!\n"
  86.              , IORpar.IOPar.io_Error );
  87.        return 10;
  88.     }
  89.  
  90.     return (return_code);
  91. }  /* set_parallel() */
  92.  
  93.  
  94.  
  95. print_request( IORpar )
  96.     struct IOExtPar *IORpar;     /* Parallel port IO request block */
  97. {
  98. #define PRINT( field )     printf( "     %s = %8lx %8ld\n"\
  99.                                  , "field", IORpar->field, IORpar->field)
  100. #define IPRINT( index, field ) printf( " %2ld  %s = %8lx %8ld\n" \
  101.                                , index, "field", IORpar->field, IORpar->field)
  102.  
  103.       printf( "index field name       hexadec  decimal\n" ); 
  104.       IPRINT( 1, io_ParFlags  );
  105.       IPRINT( 2, io_PTermArray.PTermArray0 );
  106.           PRINT( io_PTermArray.PTermArray1 );
  107.       printf( "\n" );   /* Not associated with an index */
  108.           PRINT( io_Status    );
  109. }  /* print_request() */
  110.  
  111.  
  112. print_usage() {
  113.    printf("%s: version %s\n", prog_name, prog_version );
  114.    printf("usage: %s <index> <value>\n", prog_name );
  115.    printf("  <index> is a decimal number indicating which parameter.\n" );
  116.    printf("          0 indicates print current values (and indexes) \n");
  117.    printf("            without changing them.\n" );
  118.    printf("  <value> number to set the indexed parameter to.\n");
  119.    printf("          value should be in decimal unless it starts with X\n" );
  120.    printf("          or x in which case the number should be hexadecimal\n");
  121.    exit( 5 );
  122. }
  123.  
  124.  
  125.  
  126. ULONG get_hex( s )
  127.    char *s;    /* String with hex digits */
  128. {
  129.    ULONG num=0;
  130.  
  131.    for (;*s != '\0'; s++) {
  132.       switch( *s ) {
  133.          case '0': case '1': case '2': case '3': case'4':
  134.          case '5': case '6': case '7': case '8': case'9':
  135.             num = num*16 + *s - '0'; break;
  136.          case 'a': case 'b': case 'c': case 'd': case'e': case'f':
  137.             num = num*16 + 10 + *s - 'a'; break;
  138.          case 'A': case 'B': case 'C': case 'D': case'E': case'F':
  139.             num = num*16 + 10 + *s - 'A'; break;
  140.          default: return num;    /* return what we found */
  141.       }
  142.    }
  143.  
  144.    return num;
  145. }  /* get_hex() */
  146.  
  147.  
  148. int get_dec( s )
  149.    char *s;    /* String with hex digits */
  150. {
  151.    int num=0;
  152.  
  153.    for (;*s != '\0'; s++) {
  154.       switch( *s ) {
  155.          case '0': case '1': case '2': case '3': case'4':
  156.          case '5': case '6': case '7': case '8': case'9':
  157.             num = num*10 + *s - '0'; break;
  158.          default: return num;    /* return what we found */
  159.       }
  160.    }
  161.  
  162.    return num;
  163. }  /* get_dec() */
  164.  
  165.  
  166.  
  167.  
  168. main( argc, argv ) 
  169.    int argc;
  170.    char *argv[];
  171. {
  172.    int   index;
  173.    ULONG value;
  174.  
  175.    if (argc <=0 ) { prog_name = "SetParallel"; }
  176.    else { prog_name = argv[0];}
  177.  
  178.    if (argc == 1) { print_usage(); }
  179.  
  180.    if (argc < 2 ) { printf( "Too few parameters\n" ); print_usage();}
  181.    if (argc > 3 ) { printf( "Too many parameters\n" ); print_usage();}
  182.  
  183.    index = get_dec( *++argv );
  184.  
  185.    if ((index != 0) && (argc < 3)) {
  186.       printf( "Too few parameters\n" ); print_usage();
  187.    }
  188.  
  189.    if (argc == 3) { 
  190.       ++argv;
  191.       if ((*argv[0] == 'x') || (*argv[0] == 'X')) {
  192.          value = get_hex( *argv + 1 ); /* Skip x or X */
  193.       } else {
  194.          value = get_dec( *argv );
  195.       }
  196.    }
  197.  
  198.    exit( set_parallel( index, value ) ); 
  199.    
  200. }  /* main() */
  201.  
  202.  
  203.